package com.wnnight.wys.util.repairs;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnnight.wys.entity.repairs.RepairsPlace;
import com.wnnight.wys.entity.repairs.RepairsType;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;

/**
 * @author rzh6230
 * 2022/8/27 20:32
 */
public class RepairsUtil {

    public static final ObjectMapper objectMapper = new ObjectMapper();
    public static List<RepairsType> wrapRepairsTypeList(List<RepairsType> list) {
        ArrayList<RepairsType> warpList = new ArrayList<>();
        for (RepairsType type : list) {
            if (type.getParentId() == 0) {
                warpList.add(type);
            }
        }
        getType(warpList, list);
        return warpList;
    }

    private static void getType(List<RepairsType> warpList, List<RepairsType> list) {
        if (warpList.size() == 0) {
            return;
        }
        for (RepairsType type : warpList) {
            List<RepairsType> sonType = new ArrayList<>();
            for (RepairsType m : list) {
                if (Objects.equals(m.getParentId(), type.getId())) {
                    sonType.add(m);
                }
            }
            getType(sonType, list);
            type.setSonType(sonType);
        }
    }


    public static List<RepairsPlace> wrapRepairsPlaceList(List<RepairsPlace> list) {
        ArrayList<RepairsPlace> warpList = new ArrayList<>();
        for (RepairsPlace place : list) {
            if (place.getParentId() == 0) {
                warpList.add(place);
            }
        }
        getPlace(warpList, list);
        return warpList;
    }

    private static void getPlace(List<RepairsPlace> warpList, List<RepairsPlace> list) {
        if (warpList.size() == 0) {
            return;
        }
        for (RepairsPlace place : warpList) {
            List<RepairsPlace> sonPlace = new ArrayList<>();
            for (RepairsPlace m : list) {
                if (Objects.equals(m.getParentId(), place.getId())) {
                    sonPlace.add(m);
                }
            }
            getPlace(sonPlace, list);
            place.setSonPlace(sonPlace);
        }
    }
    public static String getRecordNum() {
        return "GD" + DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
    }

    public static void sendMail(String email, String activationCode) {
        try {
            final Properties props = new Properties();
            props.put("mail.user", "623028580@qq.com");   //发件方的邮箱
            props.put("mail.password", "saidzjuarudhbcbd"); // 授权码（密码）
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.port", "25");

            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("623028580@qq.com")); // 发件人
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); // 收件人
            msg.setSubject("用户注册激活邮件-蜗牛学院"); // 主题
            msg.setContent("<h1>此邮件为蜗牛学院电商平台激活邮件！请点击下面链接完成激活操作！</h1><br />" + "此邮件30分钟有效!"
                            + "<h4><a href='http://rzh.free.svipss.top/front/user?opr=active&email=" + email + "&activationCode=" + activationCode + "'" +">http://rzh.free.svipss.top/front/user?opr=active&email=" + email + "&activationCode=" + activationCode+"</a></h4>",
                    "text/html;charset=UTF-8");
            msg.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"),
                    props.getProperty("mail.password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
